package com.hpe.fastfood.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hpe.fastfood.mapper.TypeMapper;
import com.hpe.fastfood.pojo.Bussiness;
import com.hpe.fastfood.pojo.Type;
import com.hpe.fastfood.service.TypeService;
import com.hpe.fastfood.util.Page;

@Service
public class TypeServiceImpl implements TypeService {

	@Autowired
	private TypeMapper typeMapper;

	@Override
	public Page selectType(Page page, String typeName) throws RuntimeException {
		// TODO Auto-generated method stub
		int start = (page.getCurPage() - 1) * page.getPageNumber();// 起始页
		List<Bussiness> course = typeMapper.selectType(start, page.getPageNumber(), typeName);// 分页查询记录
		// 查询一共多少条记录
		int rows = typeMapper.selectCount(typeName);
		// 计算一共多少页
		int totalPage = rows % page.getPageNumber() == 0 ? rows / page.getPageNumber()
				: rows / page.getPageNumber() + 1;
		// 封装数据到page进行放回
		page.setDate(course);
		page.setRows(rows);
		page.setTotalPage(totalPage);
		return page;
	}

	@Override
	public int checkTypeName(String typeName) throws RuntimeException {
		// TODO Auto-generated method stub
		int res = typeMapper.checkTypeName(typeName);
		if (res > 0) {// 名重复
			res = 1;
		} else {// 名不重复
			res = 0;
		}
		return res;
	}

	@Override
	public void addType(Type type) throws RuntimeException {
		// TODO Auto-generated method stub
		typeMapper.addType(type);
	}

	@Override
	public Type selectTypeById(int typeId) {
		// TODO Auto-generated method stub
		return typeMapper.selectTypeById(typeId);
	}

	@Override
	public void updateType(Type type) {
		// TODO Auto-generated method stub
		typeMapper.updateType(type);
	}

	@Override
	public void changeType(Type type) {
		// TODO Auto-generated method stub
		typeMapper.changeType(type);
	}

	@Override
	public List<Type> selectTypeInfo() throws RuntimeException {
		// TODO Auto-generated method stub
		return typeMapper.selectTypeInfo();
	}

}
